package com.bj58.move;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PlaceConfig{

	private static Map< String, String > map = null;
	private static final String PLACE_CONFIG = "D:\\place.txt";

	public PlaceConfig(){

	}

	public int getSize(){

		if( map != null ){
			return map.size();
		}
		return 0;
	}

	public String getCoordinates( String listName ){

		if( map == null ){
			try{
				loadFile( PLACE_CONFIG );
			}
			catch( IOException e ){
				return null;
			}
		}

		return map.get( listName );
	}

	private void loadFile( String path ) throws IOException{

		synchronized( PlaceConfig.class ){
			if( map == null ){
				map = new ConcurrentHashMap< String, String >();
				File file = new File( path );
				if( !file.exists() ){
					throw new FileNotFoundException( "未找到文件：" + path );
				}
				String s = null;
				BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream( file ) ) );
				while( ( s = br.readLine() ) != null ){
					String[] group = s.replaceAll( " ", "" ).split( "\\s" );
					if( group.length == 6 ){
						map.put( group[ 3 ], group[ 4 ] + "," + group[ 5 ] );
					}
				}
			}
		}
	}
}
